﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title></title>
	<link href="js-console.css" rel="stylesheet" />
	<!-- 4. Write a function that checks if a given object contains a given property
			var obj = …;
			var hasProp = hasProperty(obj,"length"); -->
</head>
<body>
	<div id="js-console"></div>
	<script src="js-console.js"></script>
	<script>
		function hasProperty(obj, propertyName) {
			var result = false;

			if (typeof obj == typeof Object()) {
				if (obj[propertyName] != undefined) {
					result = true;
				}
			}
			else {
				result = "NotAnObject";
			}

			return result;
		}

		// Test object
		var obj = {};
		obj.decription = "dummy object";
		obj.field = 123;
		obj.testFunction = function () { console.log("testFunction executed"); }

		// Use the new function
		jsConsole.writeLine("Property 'length' exists = " + hasProperty(obj, "length"));
		jsConsole.writeLine("... property 'length' added ...");
		obj.length = "variable";
		jsConsole.writeLine("Now property 'length' exists = " + hasProperty(obj, "length"));
		jsConsole.writeLine("Check property 'length' exists for primitive type = " +
								hasProperty(1, "length"));
	</script>
</body>
</html>
